package cn.itcast.bos.web.action;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Think on 2017/9/14.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven {
    private Standard standard = new Standard();

    @Override
    public Object getModel() {
        return standard;
    }

    @Autowired
    private StandardService standardService;

    @Action(value = "standard_save", results = {@Result(name = "success", type = "redirect", location = "./pages/base/standard.html")})
    public String save() {
        standardService.save(standard);
        return SUCCESS;
    }

    @Action(value = "standard_validate", results = {@Result(name = SUCCESS, type = "json")})
    public String validateName() {
        String name = standard.getName();
        List<Standard> list = standardService.findByName(name);
        for (Standard standard1 : list) {
            System.out.println(standard1);
        }
        if (list != null && list.size() == 0) {
            ServletActionContext.getContext().getValueStack().push(true);//返回true表示验证成功,没有相同用户
        } else {
            ServletActionContext.getContext().getValueStack().push(false);//返回false表示验证失败
        }
        return SUCCESS;
    }

    //注入简单参数
    private int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Action(value = "standard_pageQuery", results = {@Result(name = SUCCESS, type = "json")})
    public String pageQuery() {
        //组织分页条件
        Pageable pageable = new PageRequest(page-1,rows);
        Page<Standard> page = standardService.findPageQuery(pageable);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total",page.getTotalElements());   //总记录数
        map.put("rows",page.getContent());  //查询的一页需要的集合
        ServletActionContext.getContext().getValueStack().push(map);
        return SUCCESS;
    }

    private String ids;

    public void setIds(String ids) {
        this.ids = ids;
    }

    @Action(value = "standard_delete",results = {@Result(name = SUCCESS,location = "./pages/base/standard.html",type = "redirect")})
    public String delete(){
        //按,分隔字符串
        String[] idArray = ids.split(",");
        //调用业务层,批量作废
        standardService.deleteStandardById(idArray);
        return SUCCESS;
    }

    //快递员页面的取派标准下拉框回显
    @Action(value = "standard_findAll",results = {@Result(name = SUCCESS,type = "json")})
    public String findAll(){
        List<Standard> list = standardService.findAll();
        ActionContext.getContext().getValueStack().push(list);
        return SUCCESS;
    }
}
